SHELL=/usr/bin/env bash
# Makefile for Bichon

# Variables
GOLANG_VERSION = 1.23.3

# ANSI color codes
GREEN=$(shell tput -Txterm setaf 2)
YELLOW=$(shell tput -Txterm setaf 3)
RED=$(shell tput -Txterm setaf 1)
BLUE=$(shell tput -Txterm setaf 6)
RESET=$(shell tput -Txterm sgr0)

# Build
generate: 
	@$(MAKE) -s check-dependencies
	@$(MAKE) -s generate-golang-client
	@$(MAKE) -s generate-cpp-client
	@$(MAKE) -s generate-python-client
	
check-dependencies:
	@echo "$(YELLOW)Checking dependencies...$(RESET)"
	@$(MAKE) -s check-protoc
	@$(MAKE) -s check-protoc-gen-go
	@$(MAKE) -s check-golang
	@$(MAKE) -s check-python
	@$(MAKE) -s check-cpp
	@$(MAKE) -s check-pre-commit

install-dependencies:
	@echo "$(YELLOW)Installing dependencies...$(RESET)"
	@$(MAKE) -s check-system
	@$(MAKE) -s install-protoc
	@$(MAKE) -s install-protoc-gen-go
	@$(MAKE) -s install-golang-dependencies
	@$(MAKE) -s install-python-dependencies
	@$(MAKE) -s install-cpp-dependencies
	@$(MAKE) -s install-pre-commit-hooks
	@echo "$(GREEN)Dependencies installed successfully.$(RESET)"

generate-golang-client:
	@echo "$(YELLOW)Generating Golang client...$(RESET)"
	@protoc --cpp_out=./cpp/src/rcc/rcc.pb.h --proto_path=./proto/ ./proto/rcc.proto
	@echo "$(GREEN)C++ client generated successfully.$(RESET)"
generate-cpp-client:
	@echo "$(YELLOW)Generating C++ client...$(RESET)"
	@protoc --cpp_out=./cpp/src/rcc/rcc.pb.h --proto_path=./proto/ ./proto/rcc.proto
	@echo "$(GREEN)C++ client generated successfully.$(RESET)"
generate-python-client:
	@echo "$(YELLOW)Generating Python client...$(RESET)"
	@protoc --python_out=./python/rcc/rcc.py --proto_path=./proto/ ./proto/rcc.proto
	@echo "$(GREEN)Python client generated successfully.$(RESET)"


check-golang:
	@echo "$(YELLOW)Checking Golang installation...$(RESET)"
	@if command -v go > /dev/null; then \
		CURRENT_VERSION=$$(go version | cut -d ' ' -f 3 | cut -d 'o' -f 2); \
		if [[ "$$CURRENT_VERSION" == $(GOLANG_VERSION) ]]; then \
			echo "$(BLUE)$(shell go version) is already installed.$(RESET)"; \
		elif [[ "$$CURRENT_VERSION" < $(GOLANG_VERSION) ]]; then \
			echo "$(RED)go $(GOLANG_VERSION) is required, but $$CURRENT_VERSION is installed. suggest upgrade golang to $(GOLANG_VERSION) to continue.$(RESET)"; \
		else \
			echo "$(BLUE)$(shell go version) is already installed.$(RESET)"; \
		fi; \
	else \
		echo "$(RED)go $(GOLANG_VERSION) is not installed. Please install golang $(GOLANG_VERSION) to continue.$(RESET)"; \
		exit 1; \
	fi

check-protoc:
	@echo "$(YELLOW)Checking protoc installation...$(RESET)"
	@if command -v protoc > /dev/null; then \
		echo "$(BLUE)protoc $(shell protoc --version) is installed.$(RESET)"; \
	else \
		echo "$(RED)protoc is not installed. Please install protobuf compiler.$(RESET)"; \
		exit 1; \
	fi


install-golang-dependencies:
	@echo "$(YELLOW)Installing Golang dependencies...$(RESET)"
	@go mod tidy
	@echo "$(GREEN)Golang dependencies installed successfully.$(RESET)"

install-protoc:
	@echo "$(YELLOW)Installing protoc dependencies...$(RESET)"
	// TODO install protoc

install-protoc-gen-go:
	@echo "$(YELLOW)Installing protoc dependencies...$(RESET)"
	//TODO  install protoc-gen-go

install-python-dependencies:
	@echo "$(YELLOW)Installing Python dependencies...$(RESET)"
	// TODO install python dependencies

install-cpp-dependencies:
	@echo "$(YELLOW)Installing C ++ ...$(RESET)"
	// TODO install pre-commit hooks

install-pre-commit-hooks:
	@echo "$(YELLOW)Installing pre-commit hooks...$(RESET)"
	@git config --unset-all core.hooksPath || true
	@pip install --user pre-commit
	@export PATH="$HOME/.local/bin:$PATH"
	@pre-commit install --config $(PRE_COMMIT_CONFIG_PATH)
	@echo "$(GREEN)Pre-commit hooks installed successfully.$(RESET)"

lint:
	@echo "$(YELLOW)Running linters...$(RESET)"
	@pre-commit run --files internal/**/* --show-diff-on-failure --config $(PRE_COMMIT_CONFIG_PATH)	

# Clean up all caches
clean:
	@echo "$(YELLOW)Cleaning up caches...$(RESET)"
	@rm -rf dist
	@rm -rf logs
	@echo "$(GREEN)Caches cleaned up successfully.$(RESET)"

# Help
help:
	@echo "$(BLUE)Usage: make [target]$(RESET)"
	@echo "Targets:"
	@echo "  $(GREEN)build$(RESET)               - Build project, including environment setup and dependencies."
	@echo "  $(GREEN)check-dependencies$(RESET) - Check all system dependencies."
	@echo "  $(GREEN)clean$(RESET)             - Clean up all caches."
	@echo "  $(GREEN)install-dependencies$(RESET) - Install all project dependencies."
	@echo "  $(GREEN)lint$(RESET)              - Run linters on the project."
	@echo "  $(GREEN)run$(RESET)               - Run the application. Backend Log file will be stored in the 'logs' directory."
	@echo "  $(GREEN)test$(RESET)              - Run tests for the project."
	@echo "  $(GREEN)help$(RESET)              - Display this help message, providing information on available targets."